<?php
/*
 * Name:      Quotations
 * Directory: quotation
 * Version:   1.0.0
 * Type:      user
 * UI Name:   Quotations
 * UI Icon:
 */

// MODULE CONFIGURATION DEFINITION
$config = array();
$config['mod_name'] = 'Quotations';		// name the module
$config['mod_version'] = '1.0.0';		// add a version number
$config['mod_directory'] = 'quotations';		// tell dotProject where to find this module
$config['mod_setup_class'] = 'CSetupQuotations';	// the name of the PHP setup class (used below)
$config['mod_type'] = 'user';			// 'core' for modules distributed with dP by standard, 'user' for additional modules from dotmods
$config['mod_ui_name'] = 'Quotations';		// the name that is shown in the main menu of the User Interface
$config['mod_ui_icon'] = 'quotations.jpg';	// name of a related icon
$config['mod_description'] = 'Showing a random quote from Albert Quotations';	// some description of the module
$config['mod_config'] = true;			// show 'configure' link in viewmods
// show module configuration with the dPframework (if requested via http)
if (@$a == 'setup') {
	echo dPshowModuleConfig( $config);
}

class CSetupQuotations {
	function configure() {		// configure this module
		global $AppUI;
		$AppUI->redirect('m=quotations&a=configure');	// load module specific configuration page
  		return true;
	}

	function remove() {		// run this method on uninstall process
		echo 'call remove';
		db_exec("DROP TABLE quotations if exists;");	// remove the quotation table from database
		return null;
	}


	function upgrade($old_version) {	// use this to provide upgrade functionality between different versions; not relevant here
		switch ($old_version){
			case "all":		// upgrade from scratch (called from install)
			case "0.9":
				//do some alter table commands
			case "1.0":
				return true;
			default:
				return false;
		}
		return false;
	}

	function install() {
		$root = DP_BASE_DIR.'/files';
		$quote_folder = $root . '/quotations';
		if (!is_dir($quote_folder)) {
			if (version_compare(phpversion(), '5.0') < 0) {
				if (!is_dir($root)){
					mkdir($root, 0700);
				}
				if (!is_dir($quote_folder)){
					mkdir($quote_folder, 0700);
				}
			}
			else {
				mkdir($quote_folder, 0700, true);
			}
		}

		$sql = "CREATE TABLE `quotations` (" .
              "`quote_id` int(11) NOT NULL auto_increment, " .
              "`quote_code` varchar(255) NOT NULL, " .
              "`quote_name` varchar(255) NOT NULL, " .
              "`quote_status` int(11) NOT NULL COMMENT 'doc ky phan 3.1.3 de hieu ve status nay', " .
              "`quote_type` int(11) NOT NULL, " .
              "`quote_shipping_type` int(11) NOT NULL, " .
              "`quote_customer` int(11) NOT NULL, " .
              "`quote_supplier` int(11) NOT NULL, " .
              "`quote_remarks` varchar(11) default NULL, " .
              "`quote_completed` tinyint(3) default '0' COMMENT 'danh dau completed cua Quotation. Muc 3.1.3', " .
              "PRIMARY KEY  (`quot_id`) " .
            ") ENGINE=MyISAM DEFAULT CHARSET=latin1";
		db_exec($sql);
		// Create table line
		$sql = "CREATE TABLE `quotation_lines` (
                   `qitem_id` int(11) unsigned NOT NULL auto_increment,
                   `qitem_quotation` int(11) NOT NULL,
                   `qitem_customer` int(11) NOT NULL COMMENT 'keo quan he cua Customer xuong Item de de lookup',
                   `qitem_item` int(11) NOT NULL COMMENT 'item id lookup trong table items',
                   `qitem_remarks` varchar(255) default NULL,
                   `qitem_figure1` double default NULL,
                   `qitem_price1` double default NULL,
                   `qitem_figure2` double default NULL,
                   `qitem_price2` double default NULL,
                   `qitem_figure3` double default NULL,
                   `qitem_price3` double default NULL,
                   `qitem_figure4` double default NULL,
                   `qitem_price4` double default NULL,
                   `qitem_figure5` double default NULL,
                   `qitem_price5` double default NULL,
                   `qitem_figure6` double default NULL,
                   `qitem_price6` double default NULL,
                   `qitem_figure7` double default NULL,
                   `qitem_price7` double default NULL,
                   `qitem_figure8` double default NULL,
                   `qitem_price8` double default NULL,
                   `qitem_figure9` double default NULL,
                   `qitem_price9` double default NULL,
                   `qitem_figure10` double default NULL,
                   `qitem_price10` double default NULL,
                   PRIMARY KEY  (`qitem_id`,`qitem_quotation`)
                 ) ENGINE=MyISAM AUTO_INCREMENT=2 DEFAULT CHARSET=latin1";

		db_exec($sql);
		// Create table file
		$sql = "CREATE TABLE `quotation_files` (
                   `qfile_id` int(9) NOT NULL auto_increment,
                   `qfile_quote` int(9) NOT NULL,
                   `qfile_name` varchar(255) character set utf8 collate utf8_bin NOT NULL default '',
                   `qfile_path` varchar(255) character set utf8 collate utf8_bin default '',
                   `qfile_type` varchar(16) character set utf8 collate utf8_bin default NULL,
                   `qfile_size` varchar(32) collate latin1_general_ci default '0',
                   `qfile_date` datetime default '0000-00-00 00:00:00',
                   `qfile_version` varchar(32) character set utf8 collate utf8_bin default NULL,
                   `qfile_description` varchar(1024) character set utf8 collate utf8_bin default NULL,
                   PRIMARY KEY  (`qfile_id`)
                 ) ENGINE=MyISAM AUTO_INCREMENT=11 DEFAULT CHARSET=latin1 COLLATE=latin1_general_ci ";
		db_exec($sql);

		db_error(); // execute the queryString

		// Table lines

		// Table files
		return null;
	}

}

?>